<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div @click="goHome">
        <img src="../assets/logo.png" alt="">
        <span class="titles">unfetteredman-shop</span>
      </div>
      <el-button icon="el-icon-switch-button" circle type="info" @click="logout"></el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->   <!--判断折叠状态，控制侧边栏的宽度 -->
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <!-- 折叠按钮 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 菜单区域 unique-opened默认只展开一项   -->     <!-- collapse是否水平折叠收起菜单-->
        <el-menu :default-active='activeIndex' router  unique-opened background-color="#545c64" text-color="#fff" active-text-color="#409EFF" :collapse-transition="false" :collapse="isCollapse">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 模板图标 -->
              <i :class="menuIcon[item.id]"></i>
              <!-- 文本标题区域 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 一级菜单中的二级菜单 -->
            <el-menu-item @click="saveActiveIndex('/'+subItem.path)" :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id">
              <template slot="title">
              <!-- 模板图标 -->
              <i class="el-icon-location"></i>
              <!-- 文本标题区域 -->
              <span>{{subItem.authName}}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体区域 -->
      <el-main><router-view></router-view></el-main>
    </el-container>
</el-container>
</template>

<script>
export default {
  data () {
    return {
      menulist: [],
      menuIcon: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activeIndex: ''
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取用户列表
    async getMenuList () {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(res)
    },
    // 折叠按钮
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 将列表的url路径保存到sessionstorage里面
    saveActiveIndex (activePath) {
      window.sessionStorage.setItem('activeIndex', activePath)
      this.activeIndex = activePath
    },
    // 返回欢迎页面
    goHome () {
      this.$router.push('/home')
    }
  },
  created () {
    this.getMenuList()
    this.activeIndex = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang="less" scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color: #363C42;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: aliceblue;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
    }
    img {
      width: 55px;
      height: 55px;
      border-radius: 50%;
      box-shadow: 0px 0px 20px #eee;
    }
  }
}
.el-aside{
  background-color: #313743;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  margin: 0;
  padding: 0;
  background-color: #E9EDF1;
}
.toggle-button{
  font-size: 10px;
  line-height: 24px;
  background-color: #666666;
  text-align: center;
  color: #E9EDF1;
}
.titles{
  font-size: 20px;
  color: aquamarine;
  text-shadow: 0px 0px 20px #eee;
}
</style>
